package com.mybatisplus.demo.test;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.*;
import java.util.stream.Collectors;

public class Main {

    public static void main(String[] args) {

        Random random = new Random();

        List<Integer> integerList = new ArrayList<>();
        for (int i = 0; i < 1000000; i++) {
            integerList.add(random.nextInt(Integer.MAX_VALUE));
        }

        testStream(integerList);
        testParseStream(integerList);
        testForloop(integerList);
        testStrongFor(integerList);
        testIterator(integerList);
        System.out.println("*****************************");
        List<Good> goods = new ArrayList<>();
        for (int i = 0; i < 1000000; i++) {
            goods.add(new Good("good" + i, i, random.nextInt(Integer.MAX_VALUE)));
        }

        testGoodStream(goods);
        testGoodParseStream(goods);
        testGoodForloop(goods);
        testGoodIterator(goods);
        testGoodStrongFor(goods);

        List<Integer> integers = new ArrayList<>();
        for (int i = 0; i < 1000; i++) {
            integers.add(i);
        }
        System.out.println(integers.size());
        List<Integer> a = new ArrayList<>();
        List<Integer> a1 = new ArrayList<>();
        List<Integer> a2 = new ArrayList<>();

        integers.stream().forEach(x -> a.add(x));
        System.out.println(a.size());

        integers.parallelStream().forEach(x -> a1.add(x));
        System.out.println(a1.size());

        a2 = integers.parallelStream().collect(Collectors.toList());
        System.out.println(a2.size());
    }

    public static void testStream(List<Integer> list) {
        long start = System.currentTimeMillis();
        Optional<Integer> max = list.stream().max(Integer::compare);
        System.out.println(max.get());
        long end = System.currentTimeMillis();
        System.out.println("testStream :" + (end - start) + "ms");
    }

    public static void testParseStream(List<Integer> list) {
        long start = System.currentTimeMillis();
        Optional<Integer> max = list.parallelStream().max(Integer::compare);
        System.out.println(max.get());
        long end = System.currentTimeMillis();
        System.out.println("testParseStream :" + (end - start) + "ms");
    }

    public static void testForloop(List<Integer> list) {
        long start = System.currentTimeMillis();
        int max = Integer.MAX_VALUE;
        for (int i = 0; i < list.size(); i++) {
            int current = list.get(i);
            if (current > max) {
                max = current;
            }
        }
        System.out.println(max);
        long end = System.currentTimeMillis();
        System.out.println("testForloop :" + (end - start) + "ms");
    }

    public static void testStrongFor(List<Integer> list) {
        long start = System.currentTimeMillis();
        int max = Integer.MAX_VALUE;
        for (Integer integer : list) {
            if (integer > max) {
                max = integer;
            }
        }
        System.out.println(max);
        long end = System.currentTimeMillis();
        System.out.println("testStrongFor :" + (end - start) + "ms");
    }

    public static void testIterator(List<Integer> list) {
        long start = System.currentTimeMillis();
        Iterator<Integer> it = list.iterator();
        int max = it.next();
        while (it.hasNext()) {
            int current = it.next();
            if (current > max) {
                max = current;
            }
        }
        System.out.println(max);
        long end = System.currentTimeMillis();
        System.out.println("testIterator :" + (end - start) + "ms");
    }

//---------------------------------------------------------------------

    public static void testGoodStream(List<Good> list) {
        long start = System.currentTimeMillis();

        Optional<Good> max = list.stream().max(Comparator.comparingInt(Good::getHot));
        System.out.println(max.get());

        long end = System.currentTimeMillis();
        System.out.println("testStream :" + (end - start) + "ms");
    }

    public static void testGoodParseStream(List<Good> list) {
        long start = System.currentTimeMillis();
        Optional<Good> max = list.parallelStream().max(Comparator.comparingInt(Good::getHot));
        System.out.println(max.get());
        long end = System.currentTimeMillis();
        System.out.println("testParseStream :" + (end - start) + "ms");
    }

    public static void testGoodForloop(List<Good> list) {
        long start = System.currentTimeMillis();
        Good good = list.get(0);
        for (int i = 0; i < list.size(); i++) {
            Good current = list.get(i);
            if (current.getHot() > good.getHot()) {
                good = current;
            }
        }
        System.out.println(good);
        long end = System.currentTimeMillis();
        System.out.println("testForloop :" + (end - start) + "ms");
    }

    public static void testGoodStrongFor(List<Good> list) {
        long start = System.currentTimeMillis();
        Good good = list.get(0);
        for (Good integer : list) {
            if (integer.getHot() > good.getHot()) {
                good = integer;
            }
        }
        System.out.println(good);
        long end = System.currentTimeMillis();
        System.out.println("testStrongFor :" + (end - start) + "ms");
    }

    public static void testGoodIterator(List<Good> list) {
        long start = System.currentTimeMillis();
        Iterator<Good> it = list.iterator();
        Good good = it.next();
        while (it.hasNext()) {
            Good current = it.next();
            if (current.getHot() > good.getHot()) {
                good = current;
            }
        }
        System.out.println(good);
        long end = System.currentTimeMillis();
        System.out.println("testIterator :" + (end - start) + "ms");
    }


}

@AllArgsConstructor
@Data
class Good {
    private String name;

    private Integer stock;

    private Integer hot;
}